<template>
  <div class="sure_order">
    <div class="address" :addlister="addlister">
      <div class="address_img">
        <van-icon name="location-o" />
      </div>
      <div class="address_masg">
        <p>{{ addlister.area_name }}</p>
        <p>
          {{ addlister.name }} <span>{{ addlister.phone }}</span>
        </p>
      </div>
      <div class="address_arrow" @click="goaddress">
        <van-icon name="arrow" />
      </div>
    </div>

    <div class="shop_masg" v-for="item in shoplist" :key="item.id">
      <van-card
        class="name_masg"
        :num="item.num"
        :price="item.s_good.price"
        :desc="item.s_good.desc"
        :title="item.s_good.name"
        :thumb="item.s_good.s_goods_photos[0].path"
      >
      </van-card>
    </div>

    <div class="detail">
      <div class="momey_detail">价格明细</div>
      <div class="all_shop">
        <p>共商品</p>
        <span>{{ shoplist.length }}件</span>
      </div>
      <div class="all_money">
        <p>合计</p>
        <span>￥{{ all }}</span>
      </div>
    </div>

    <div class="payfor">
      <van-cell title="支付方式" />
      <van-cell title="微信" icon="gold-coin-o" class="chouse">
        <van-radio v-model="checked" name="1" class="chouse_right"></van-radio>
      </van-cell>
      <van-cell title="支付宝" icon="gold-coin-o" class="chouse">
        <van-radio v-model="checked" name="2" class="chouse_right"></van-radio>
      </van-cell>
      <van-cell title="银行卡" icon="gold-coin-o" class="chouse">
        <van-radio v-model="checked" name="3" class="chouse_right"></van-radio>
      </van-cell>
    </div>
    <van-submit-bar
      :price="all * 100"
      button-text="提交订单"
      @submit="onSubmit"
      class="name"
    />
  </div>
</template>

<script setup>

import { Icon } from 'vant';
import { RadioGroup, Radio } from 'vant';
import { computed, ref, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
let router = useRouter()
let route = useRoute()
import { get, post } from '../../utils/request.js'


// 拿仓库的数据
import { useStore } from 'vuex'
const store = useStore()
let shoplist = store.state.user.list
console.log(shoplist);


// 总额
let all = computed(() => {
  let money = 0
  shoplist.forEach(ele => {
    money += ele.s_good.price * ele.num
  });
  return money
})

// 支付方式选择按钮
let checked = ref(true);


// 地址
const goaddress = () => {
  router.push('/Address')
}

// 渲染地址
let addlister = ref({})
nextTick(() => {
  get('/defalutAddress').then(res => {
    console.log(res);
    addlister.value = res.result
  })
})


let shop = store.state.user.list
console.log(shop);

function shops (arr) {
  let newArr = []
  arr.forEach(ele => {
    let obj = {}
    obj.id = ele.goods_id
    obj.num = ele.num
    newArr.push(obj)
  })
  let num = []
  newArr.forEach(ele => {
    num.push(ele.id)
  })

  return { newArr, num }
}


// 提交订单
const onSubmit = async () => {
  let data = {
    addr_id: addlister.value.id,
    goods_info: shops(shop).newArr,
    project_id: 242,
    shoppingCartIds: shops(shop).num,
  }
  console.log(data)
  let res = await post('/order', data)
  console.log(res);
  router.push('/Order')
}


</script>

<style lang="scss" scoped>
.sure_order {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: rgb(214, 209, 209);
}

.address {
  display: flex;
  width: 3.79rem;
  height: 1rem;
  margin: 0.2rem 0;
  border-radius: 0.2rem;
  background-color: white;
  padding: 0.1rem;

  .address_img {
    width: 0.4rem;
    height: 3.79rem;
    border-bottom-left-radius: 0.12rem;
    border-top-left-radius: 0.12rem;
    font-size: 0.4rem;
    text-align: center;
    line-height: 1rem;
    color: orange;
  }

  .address_masg {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    width: 2.8rem;
    margin: 0 0.12rem;
    height: 100%;

    p {
      font-size: 0.2rem;
    }
    p:last-child {
      font-size: 0.16rem;
      color: rgb(164, 166, 168);
    }

    span {
      font-size: 0.16rem;
      color: rgb(164, 166, 168);
    }
  }

  .address_arrow {
    width: 0.4rem;
    height: 100%;
    border-bottom-right-radius: 0.12rem;
    border-top-right-radius: 0.12rem;

    font-size: 0.3rem;
    text-align: center;
    line-height: 1rem;
  }
}

.shop_masg {
  margin: 0.1rem 0;
  padding: 0.1rem;
  width: 3.79rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: white;
  border-radius: 0.1rem;

  .name_masg {
    border-radius: 0.1rem;
  }
}

.detail {
  margin: 0.2rem 0;
  width: 3.79rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background-color: white;
  font-size: 0.18rem;
  border-radius: 0.1rem;
  padding: 0.1rem;

  .all_shop {
    margin-top: 0.2rem;
    display: flex;
    width: 100%;
    height: 0.3rem;
    line-height: 0.3rem;
    justify-content: space-between;

    p {
      font-size: 0.19rem;
    }

    span {
      font-size: 0.22rem;
    }
  }

  .all_money {
    display: flex;
    height: 0.3rem;
    width: 100%;
    margin-top: 0.2rem;
    line-height: 0.3rem;
    justify-content: space-between;
    p {
      font-size: 0.19rem;
    }

    span {
      font-size: 0.22rem;
    }
  }
}

.payfor {
  margin: 0.2rem 0;
  width: 3.79rem;
  //   padding: 0.1rem;
  padding-bottom: 0.5rem;

  p {
    font-size: 0.16rem;
    margin: 0.1rem;
  }

  .chouse {
    position: relative;

    .chouse_right {
      position: absolute;
      right: 0;
    }
  }
}

.name {
  width: 100%;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}
</style>